package com.hp.bon.sgw.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.sql.SQLException;
import java.util.Arrays;

import org.h2.tools.Server;

public class H2DBDemoServer {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(H2DBDemoServer.class);

	private Server server = null;
	private boolean startDB = true;

	private String[] args = null;

	public void setStartDB(boolean startDB) {
		this.startDB = startDB;
	}

	public void start() throws SQLException {
		if (!startDB) {
			return;
		}
		if (args == null) {
			String baseDir = System.getProperty("SGW_HOME") + File.separator + "data";
			args = new String[] { "-tcp", "-baseDir", baseDir, "-tcpAllowOthers", "-webAllowOthers", "-tcpPort", "4900" };

		}
		logger.info("start SGW Data Server ,args:" + Arrays.toString(args));
		try {
			org.h2.tools.Server server = Server.createTcpServer(args);
			server.start();
		} catch (Exception e) {
			System.out.println("start SGW Data Server err:" + e.toString());
		}
		System.out.println("start SGW Data Server finished ");
	}

	public void stop() {
		server.stop();
	}

	public void setArgs(String args) {
		if (args != null) {
			this.args = args.split(" ");
		}

	}

}
